package es.udc.fic.hamNeggs.dao;

import es.udc.fic.hamNeggs.manager.MeetingManager;
import es.udc.fic.hamNeggs.manager.SprintManager;
import es.udc.fic.hamNeggs.manager.UserStoriesManager;
import es.udc.fic.hamNeggs.manager.WorksManager;
import es.udc.fic.hamNeggs.pojo.Meeting;
import es.udc.fic.hamNeggs.pojo.Project;
import es.udc.fic.hamNeggs.pojo.Sprint;
import es.udc.fic.hamNeggs.pojo.UserStories;
import es.udc.fic.hamNeggs.pojo.Works;
import es.udc.fic.hamNeggs.pojo.Works.Rol;

import java.sql.Date;
import java.util.ArrayList;

public class InputProject {
    private String            id;

    private String            name;

    private String            dateStart;

    private String            dateEnd;

    private String            scrumMaster;

    private String            scrumMasterID;

    private String            currentRol;

    private ArrayList<String> userStories = new ArrayList<String>();

    private ArrayList<String> sprints     = new ArrayList<String>();

    private ArrayList<String> meetings    = new ArrayList<String>();

    private ArrayList<String> worker      = new ArrayList<String>();

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDateStart() {
        return dateStart;
    }

    public void setDateStart(String dateStart) {
        this.dateStart = dateStart;
    }

    public String getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(String dateEnd) {
        this.dateEnd = dateEnd;
    }

    public ArrayList<String> getUserStories() {
        return userStories;
    }

    public void setUserStories(ArrayList<String> userStories) {
        this.userStories = userStories;
    }

    public ArrayList<String> getSprints() {
        return sprints;
    }

    public void setSprints(ArrayList<String> sprints) {
        this.sprints = sprints;
    }

    public void setMeetings(ArrayList<String> meetings) {
        this.meetings = meetings;
    }

    public ArrayList<String> getWorker() {
        return worker;
    }

    public void setWorker(ArrayList<String> worker) {
        this.worker = worker;
    }

    public int getUserStoriesSize() {
        return userStories.size();
    }

    public String getCurrentRol() {
        return currentRol;
    }

    public void setCurrentRol(String currentRol) {
        this.currentRol = currentRol;
    }

    public int getUserStoriesDone() {
        int count = 0;

        UserStoriesManager usrmng = new UserStoriesManager();

        for (String userStory : userStories) {
            UserStories ust = usrmng.getUserStories(new Long(userStory));
            if (ust.getState().getString().equalsIgnoreCase("Validated"))
                count++;
        }

        usrmng.close();

        return count;
    }

    public Project toProject() {
        Project project = new Project();
        WorksManager wrkmng = new WorksManager();
        UserStoriesManager ustmng = new UserStoriesManager();
        SprintManager sprmng = new SprintManager();
        MeetingManager mtgmng = new MeetingManager();

        if (id != null && id != "")
            project.setId(new Long(id));
        if (name != null && name != "")
            project.setName(name);
        if (dateStart != null && dateStart != "")
            project.setDateStart(Date.valueOf(dateStart));
        if (dateEnd != null && dateEnd != "")
            project.setDateEnd(Date.valueOf(dateEnd));
        if (worker != null && !worker.isEmpty())
            for (String work : worker)
                project.setOneWorker(wrkmng.getWorks(new Long(work), new Long(id)));
        if (userStories != null && !userStories.isEmpty())
            for (String ust : userStories)
                project.setOneUserStory(ustmng.getUserStories(new Long(ust)));
        if (sprints != null && !sprints.isEmpty())
            for (String spr : sprints)
                project.setOneSprint(sprmng.getSprint(new Long(spr)));
        if (meetings != null && !meetings.isEmpty())
            for (String mgr : meetings)
                project.setOneMeeting(mtgmng.getMeeting(new Long(mgr)));

        wrkmng.close();
        ustmng.close();
        sprmng.close();
        mtgmng.close();

        return project;
    }

    public void setProject(Project project) {
        if (project.getId() != null)
            id = project.getId().toString();
        if (project.getName() != null)
            name = project.getName();
        if (project.getDateStart() != null)
            dateStart = project.getDateStart().toString();
        if (project.getDateEnd() != null)
            dateEnd = project.getDateEnd().toString();
        if (project.getWorker() != null && !project.getWorker().isEmpty())
            for (Works work : project.getWorker()) {
                worker.add(work.getUser().getId().toString());
                if (work.getRol().equals(Rol.ScrumMaster)) {
                    scrumMaster = work.getUser().getName();
                    scrumMasterID = work.getUser().getId().toString();
                }
            }
        if (project.getUserStories() != null && !project.getUserStories().isEmpty())
            for (UserStories ust : project.getUserStories())
                userStories.add(ust.getId().toString());
        if (project.getSprints() != null && !project.getSprints().isEmpty())
            for (Sprint spr : project.getSprints())
                sprints.add(spr.getId().toString());
        if (project.getMeetings() != null && !project.getMeetings().isEmpty())
            for (Meeting mtg : project.getMeetings())
                meetings.add(mtg.getId().toString());
    }

    public String getScrumMaster() {
        return scrumMaster;
    }

    public void setScrumMaster(String scrumMaster) {
        this.scrumMaster = scrumMaster;
    }

    public ArrayList<String> getMeetings() {
        return meetings;
    }

    public String getScrumMasterID() {
        return scrumMasterID;
    }

    public void setScrumMasterID(String scrumMasterID) {
        this.scrumMasterID = scrumMasterID;
    }

}
